package cn.com.infcn.util.mss;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.nutz.http.Http;
import org.nutz.http.Response;

import cn.com.infcn.po.Restful;


/**
 * 
 * 描述: 调用msp的client
 *
 * @author xiep
 * @date 2017年11月20日 上午10:47:49
 */
public class MssClient {
	private String ip;
	private int port;
	private int timeout;
	private boolean is_msp;
	private String sousuoFenMianClassName = "MssSearchApi";
	private String mssDataApiClassName = "MssDataApi";
	
	private MssSearchApi mssSearchApi=null;
	
	public MssClient(String ip, int port, int timeout,boolean is_msp) {
		this.ip = ip;
		this.port = port;
		this.timeout = timeout;
		this.is_msp=is_msp;
	}
	public MssClient(MssSearchApi mssSearchApi) {
		this.mssSearchApi=mssSearchApi;
	}

	/**
	 * 根据ID查询msp的数据
	 * 
	 * @param name
	 * @param id
	 * @param _table_src
	 * @return
	 * @throws Exception
	 */
	public Restful mssDataApi_findById(String name, String id, String _table_src) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("name", name);
		params.put("id", id);
		params.put("_table_src", _table_src);
		Response post = this.post(mssDataApiClassName, "findById", params);
		return Restful.instance(post);
	}

	/**
	 * 
	 * 描述: 分面方法
	 *
	 * @param tableNames
	 * @param featchSize
	 * @param size
	 * @param query
	 * @param filter
	 * @param fields
	 * @return
	 * @throws Exception
	 *             Response
	 */
	public Restful mssSearchApi_aggregationsByQuery(String tableNames, String featchSize, int size, String query,
			String filter, String fields) throws Exception {
		Restful returnObj =null;
		if(is_msp) {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("tableNames", tableNames);
			params.put("featchSize", featchSize);
			params.put("size", size);
			params.put("query", query);
			if (StringUtils.isNotBlank(filter)) {
				params.put("filter", filter);
			}
			params.put("field", fields);
			
			Response post = this.post(sousuoFenMianClassName, "aggregationsByQuery", params);
			returnObj= Restful.instance(post);
		}else {
			Restful restful = mssSearchApi.aggregationsByQuery(tableNames, Integer.parseInt(featchSize), size, query,filter , fields.split(","));
			returnObj=restful;
		}

		return returnObj;
	}

	public Restful mssSearchApi_searchByQuery(String tableNames, int from, int size, String defaultOperator,
			String query, String filter, String heighlight, String includes, String excludes, String distinct,
			String sort) throws Exception {
		Restful returnObj=null; 
		if (is_msp) {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("tableNames", tableNames);
			params.put("from", from);
			params.put("size", size);
			params.put("defaultOperator", defaultOperator);
			params.put("query", query);
			if (StringUtils.isNotBlank(filter)) {
				params.put("filter", filter);
			}
			if (StringUtils.isNotBlank(heighlight)) {
				params.put("heighlight", heighlight);
			}
			if (StringUtils.isNotBlank(includes)) {
				params.put("includes", includes);
			}
			if (StringUtils.isNotBlank(excludes)) {
				params.put("excludes", excludes);
			}
			if (StringUtils.isNotBlank(distinct)) {
				params.put("distinct", distinct);
			}
			if (StringUtils.isNotBlank(sort)) {
				params.put("sort", sort);
			}
			Response response= this.post(sousuoFenMianClassName, "searchByQuery", params);
			returnObj=Restful.instance(response);
		
		} else {
			Restful searchByQuery = mssSearchApi.searchByQuery(tableNames, from, size, defaultOperator, query, filter, heighlight, includes, excludes, distinct, sort, false);
			returnObj=searchByQuery;
		}
		return returnObj;
	}

	/**
	 * 
	 * 超时使用类的
	 *
	 * @param methodName
	 * @param params
	 * @return
	 * @throws MalformedURLException
	 * @throws IOException
	 *             Response
	 */
	public Response post(String className, String methodName, Map<String, Object> params)
			throws MalformedURLException, IOException {
		return this.post(className, methodName, timeout, params);
	}

	/**
	 * 通过post方式获取流
	 * 
	 * @param className
	 * @param methodName
	 * @param params
	 * @return
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public Response post(String className, String methodName, int timeout, Map<String, Object> params)
			throws MalformedURLException, IOException {
		StringBuilder sb = new StringBuilder("http://" + ip + ":" + port + "/api/" + className + "/" + methodName);

		int sblength = sb.toString().length();
		Set<Entry<String, Object>> entrySet = params.entrySet();
		for (Entry<String, Object> entry : entrySet) {
			String key = entry.getKey();
			Object value = entry.getValue();
			if (sblength == sb.toString().length()) {
				sb.append("?").append(key).append("=").append(value).append("&");
				System.out.println("\t" + key + ":" + value);
			} else {
				sb.append(key).append("=").append(value).append("&");
				System.out.println("\t" + key + ":" + value);
			}
		}
		if (sb.toString().endsWith("&")) {
			sb.deleteCharAt(sb.toString().length() - 1);
		}

		System.err.println(sb.toString());
		System.err.println();
		return Http.post2("http://" + ip + ":" + port + "/api/" + className + "/" + methodName, params, timeout);
	}

}
